package com.yikaiye.android.yikaiye.ui.activity.mine;

import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityPdfBinding;
import com.yikaiye.android.yikaiye.di.retrofit.ApiConstants;
import com.yikaiye.android.yikaiye.model.InvoiceDetailsModel;

import java.util.List;

/**
 * 发票pfd
 */
public class PdfActivity extends BaseActivity<ActivityPdfBinding, BaseViewModel> {

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.e_invoice));
        if (getIntent().getExtras() != null) {
            List<InvoiceDetailsModel.FileModel> fileList = (List<InvoiceDetailsModel.FileModel>) getIntent().getExtras().getSerializable(Constant.MODEL_KEY);
            if (fileList != null) {
                for (InvoiceDetailsModel.FileModel fileModel : fileList) {
                    fileModel.setFilePath(ApiConstants.BASE_PDF_URL + fileModel.getFilePath());
                }
            }
            // 启用 JavaScript
            binding.webView.getSettings().setJavaScriptEnabled(true);
            // 不用跳转到外部浏览器
            binding.webView.setWebViewClient(new WebViewClient());
            String baseUrl = "https://wx-link.yikaiye.net/mobile-pdf-viewer.html?invoiceFileList=";
            binding.webView.loadUrl(baseUrl + new Gson().toJson(fileList));
        }
    }


//    /**
//     * 通过链接下载pdf
//     *
//     * @param pdfUrl pdf下载链接
//     */
//    private void downloadPdf(String pdfUrl) {
//        final String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
//        DownLoadHelper.getInstance().download(pdfUrl, filePath, new DownLoadHelper.OnDownloadListener() {
//            @Override
//            public void onDownloadSuccess(String path) {
////                dismissLoadingDialog();
//                binding.pdfView.fromFile(new File(path)).defaultPage(0) // 默认显示第一页
//                        .enableSwipe(true) // 启用滑动翻页
//                        .swipeHorizontal(false) // 水平滑动翻页
//                        .load();
//            }
//
//            @Override
//            public void onDownloading(int progress) {
////                showLoadingDialog();
//            }
//
//            @Override
//            public void onDownloadFailed() {
////                dismissLoadingDialog();
//            }
//        });
//    }
}
